import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    navs: [],
    goods: [],
    showlist: [],
    hotWords: ["侃爷", "宇智波佐助", "元龙", "LPL", "粽子", "柯南", "杭州女子", "周深 无限", "哈利波特混剪", "明日方舟", "创造营2020"],
    history: JSON.parse(localStorage.getItem('history')) || [],
    mind: []
  },
  mutations: {
    getNav(state, item) {
      state.navs = [...item];
    },
    getData(state, item) {
      state.goods = [...item];
      state.showlist = [...item]
    },
    onChange(state, name) {
      state.showlist = [];
      state.goods.map(ele => {
        if (ele.cid === name) {
          state.showlist.push(ele);
        }
      })
    },
    onSearch(state, val) {
      state.showlist = []
      state.showlist = [...state.mind];
      state.history.push(val);
      localStorage.setItem('history', JSON.stringify(state.history));
    },
    onInput(state, value) {
      state.mind = [];
      state.goods.forEach(ele => {
        if (ele.title.includes(value)) {
          state.mind.push(ele);
        }
      })
    },

    del(state) {
      state.history = [];

      localStorage.setItem('history', JSON.stringify(state.history));
    }
  },
  actions: {
    getNav(context) {
      axios.get("http://localhost:8080/fenlei.json")
        .then(res => {
          context.commit('getNav', res.data.result)
        })
    },
    getData(context) {
      axios.get("http://localhost:8080/list.json")
        .then(res => {
          context.commit('getData', res.data.result)
        })
    },
    onChange(context, name) {
      context.commit('onChange', name)
    },
    onSearch(context, val) {
      context.commit('onSearch', val);
    },
    onInput(context, value) {
      context.commit('onInput', value)
    },
    del(context) {
      context.commit('del')
    }
  },
  modules: {
  }
})
